<?php

namespace app\admin\controller\xiangmuzu;

use app\admin\controller\Controller;
use app\admin\model\xiangmuzu\Article as ArticleModel;
use think\Db;
use think\Session;

/**
 * 项目组控制器
 */
class Article extends Controller{
    public function index(){
        $model = new ArticleModel;
        $list = $model->getList($this->request->get());
        return $this->fetch('index', compact('list'));
    }
    /**
     * 添加项目组
     */
    public function add(){
        $model = new ArticleModel;
        if(empty($this->postData('company')['name'])){
            //$catgory = Db::name('yonghui_user')->where(['companyId'=>Session::get('yoshop_adminuser')['user']['company_id'],'type'=>1,'status'=>1])->select();
            return $this->fetch('add', compact('model'));
        }
        $data=$this->postData('company');
      /*  if(empty($data['onSiteId'])){
            return $this->renderError($model->getError() ?: '请选择驻场人员');
        }*/
        //判断该项目组已添加过
        $userInfo=Db::name('yonghui_project')->where(['name'=>$data['name'],'companyId'=>Session::get('yoshop_adminuser')['user']['company_id']])->find();
        if($userInfo){
            return $this->renderError($model->getError() ?: '该项目组名称已被添加过');
        }
        //所属公司id
        $data['companyId']=Session::get('yoshop_adminuser')['user']['company_id'];
        $data['createTime']=time();
        $data['status']=1;
        $insert=$model->add($data);
        if($insert){
            return $this->renderSuccess('添加成功', url('xiangmuzu.article/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }



    /**
     * 更新项目组信息
     */
    public function edit($id){
        //人员详情
        $model = ArticleModel::detail($id);
        if (!$this->request->isAjax()){
            //驻场人员
            //$catgory = Db::name('yonghui_user')->where(['companyId'=>Session::get('yoshop_adminuser')['user']['company_id'],'type'=>1,'status'=>1])->select();
            //var_dump($catgory);
            return $this->fetch('edit', compact('model'));
        }
        $data=$this->postData('company');
       /* if(empty($data['onSiteId'])){
            return $this->renderError($model->getError() ?: '请选择驻场人员');
        }*/
        //判断该项目组已添加过
        $userInfos=Db::name('yonghui_project')->where(['name'=>$data['name'],'companyId'=>Session::get('yoshop_adminuser')['user']['company_id']])->find();
        if($userInfos&&$id!=$userInfos['id']){
            return $this->renderError($model->getError() ?: '该项目组名称已添加过');
        }
        $data['updateTime']=time();
        if ($model->edit($data)) {
            return $this->renderSuccess('更新成功', url('xiangmuzu.article/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除项目组
     */
    public function delete($id){
        $model = ArticleModel::detail($id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }
    /*
     * 恢复
     */
    public function recover($id){
        $model = ArticleModel::detail($id);
        if (!$model->recover()) {
            return $this->renderError($model->getError() ?: '恢复失败');
        }
        return $this->renderSuccess('恢复成功');
    }
}